package com.zjw.concurrency.aqs;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 模拟运动会，裁判员发令枪响后，运动员出发起跑
 * */
public class CountDownLatchExample2 {

    private static CountDownLatch judgerLatch = new CountDownLatch(1);

   // private static CountDownLatch playerLatch = new CountDownLatch(5);


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 5; i++) {
            int finalI = i;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        judgerLatch.await();
                        System.out.println("运动员" + finalI +"号开始起跑");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });

            executorService.submit(thread);
        }
        System.out.println("比赛开始");
        judgerLatch.countDown();
        executorService.shutdown();
    }
}
